﻿using System;
using System.Reflection;

namespace Moonlite.Data.Configuration
{
    /// <summary>
    /// Represents a database for <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> pairs.
    /// </summary>
    internal static class AttachmentRegister
    {
        static AssemblyConfigurationAttachmentCollection pairs = new AssemblyConfigurationAttachmentCollection();

        /// <summary>
        /// Gets the <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> pairs.
        /// </summary>
        /// <value>The <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> pairs.</value>
        internal static AssemblyConfigurationAttachmentCollection Pairs
        {
            get { return pairs; }
        }

        /// <summary>
        /// Registers <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> the pair.
        /// </summary>
        /// <param name="pair">The <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> pair.</param>
        public static void RegisterPair(AssemblyConfigurationAttachment pair)
        {
            RegisterPair(pair, true);
        }

        /// <summary>
        /// Registers <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> the pair.
        /// </summary>
        /// <param name="pair">The <see cref="System.Reflection.Assembly"/>/<see cref="ConfigurationInstance"/> pair.</param>
        /// <param name="load">Whether the pair should be loaded instantly.</param>
        public static void RegisterPair(AssemblyConfigurationAttachment pair, bool load)
        {
            if (!pairs.Contains(pair))
            {
                pairs.Add(pair);
                if (load)
                {
                    AssemblyLoadSpy.LoadAssemblyConfigurationAttachment(pair);
                }
            }
        }
    }
}
